package cn.xd.security.service;

import cn.hutool.core.util.StrUtil;
import cn.xd.base.application.service.query.AdminUserQueryAppService;
import cn.xd.base.domain.bean.response.AdminUserForLoginDTO;
import cn.xd.security.userDetail.AdminUserDetails;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service("AdminUserDetailServiceImpl")
@RequiredArgsConstructor
public class AdminUserDetailServiceImpl implements UserDetailsService, UserDetailsPasswordService {
    private final AdminUserQueryAppService adminUserQueryAppService;

    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String userName = StrUtil.subBefore(username, "@", Boolean.TRUE);
        String org = StrUtil.subAfter(username, "@", Boolean.TRUE);
        AdminUserForLoginDTO dto = adminUserQueryAppService.getInfoByAccount(userName, Long.parseLong(org)).isEmptyThrow("未找到用户");
        return AdminUserDetails.newAdminUserDetails(dto, Long.parseLong(org));
    }
}
